require "proto/player/message/ReqShareRewardMessage"
require "proto/player/message/ReqShareInfoMessage"

ShareManager        = {}

local m_today       = 0
local MAX_SHARE     = tonumber(G.dataTable["t_global"]["536"].f_int_value);
local m_shareType   = 0
local m_qrcode_url  = nil

--微信分享回调
function ShareManager.OnWXShareCallBack(errCode)
    logError("微信分享回调")
    logError("微信分享:" .. errCode)
    if errCode == 0 then
        closeUI(PanelResNames.UIShareDraw)
        closeUI(PanelResNames.UIShareHero)
    elseif errCode == 1 then

    end
end

--微博分享回调
function ShareManager.OnWBShareCallBack(code)
    logError("微博分享回调")
    logError("微博分享:" .. code)
    if code == 0 then       --成功
    elseif code == 1 then   --失败
    elseif code == 2 then   --取消
    end
end

function ShareManager.Req_shareReward(hid)
    local t = ShareManager.GetShareType()
    if t == EnumConst.ShareType.shareHero then
        if m_today < MAX_SHARE then
            local req = ReqShareRewardMessage.New()
            sendReqMessage(req)
        end
    else
        if m_today < MAX_SHARE then
            local req = ReqShareRewardMessage.New()
            sendReqMessage(req)
        end
    end
end

function ShareManager.HaveReward()
    return m_today < MAX_SHARE
end

function ShareManager.Res_shareReward(msg)
    if msg.state == 0 then
        utimer.delayCall(function()
            logicMgr.UIShowManager.ShowRewardByTempItemInfo(msg.rewards)
        end, 1500)
        
        m_today = m_today + 1
    end
end

function ShareManager.Res_shareTimes(msg)
    if msg.state == 0 then
        m_today = msg.times
    end
end

function ShareManager.Req_shareTimes()
    local req = ReqShareInfoMessage.New()
    sendReqMessage(req)
end

function ShareManager.SetShareType(type)
    m_shareType = type
end

function ShareManager.GetShareType()
    return m_shareType
end

function ShareManager.GetDesc()
    local type = ShareManager.GetShareType()
    if type == EnumConst.ShareType.shareHero then
        return getLanguage("ShareText2")
    else
        return getLanguage("ShareText1")
    end

end

function ShareManager.GetQrcodeUrl(callback,newUrl)
    local android_url = Config.ossUrl.."qrcode/android/" .. Config.GroupId.."/qrcode.txt"
    local ios_url = Config.ossUrl.."qrcode/ios/".. Config.GroupId .. "/qrcode.txt"
    
    local url
    if Application.platform == RuntimePlatform.Android then
        url = android_url
    elseif Application.platform == RuntimePlatform.IPhonePlayer then
        url = ios_url
    else
        url = android_url
    end

    if newUrl then
        url = newUrl
    end

    if m_qrcode_url then
        if callback then
            callback(m_qrcode_url)
        end
    else
        Http.Get(url, nil, function(success, str)
            if not success or string.IsNilOrEmpty(str) then
                logError("二维码地址为空！")
                return
            end
            local ls = decodeJsonStrToTabel(str)
            for _,v in pairs(ls) do
                if v.channel == Config.SDK_Channel then
                    m_qrcode_url = v.url
                    if callback then
                        callback(m_qrcode_url)
                    end
                end
            end
        end)
    end
end

return ShareManager